<?php !defined('BASE_ROOT') && exit ( 'Access Denied!' );
class Router {

	/**
	 * Controller和Action
	 *
	 * @var string
	 */
	public $controller;
	public $acton;

	/**
	 * Controller的文件名
	 *
	 * @var string
	 */
	private $controllerFile;

	/**
	 * Controller的类名
	 *
	 * @var string
	 */
	public $controllerClass;

	/**
	 * URL参数
	 *
	 * @var array
	 */
	public $param = array();
	/**
	 * 阻止的Action 防止跟已有类名重复
	 *
	 * @var array
	 */
	private $blockAction = array('database', 'httprequest', 'model', 'controller', 'template', 'basic','router','input','exceptions');
	/**
	 * 阻止的ActionDo 防止跟已有类成员重复
	 *
	 * @var array
	 */
	private $blockActionDo = array('isaccess','islogin','assign','show','getruntime','get_instance','model','plugin','helper','config','database');

	/**
	 * 构造函数.
	 *
	 * @return void
	 * @access public
	 *
	 */
	public function __construct() {
		try {
			$this->parsePath();
			$this->getControllerFile();
			$this->getControllerClass();
		} catch (Exception $e) {
			show_404();
		}
	}

	/**
	 * 解析URL路径
	 *
	 * @return void
	 * @access private
	 *
	 */
	private function parsePath(){
		//非APACHE空间使用$_SERVER['ORIG_PATH_INFO']
		$uri = isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:$_SERVER['ORIG_PATH_INFO'];
		$url = str_ireplace(array('/index.php',str_ireplace('/index.php', '', $_SERVER['SCRIPT_NAME'])),'',$uri);
		$raw_param	= explode('?',$url);
		$first_param = trim($raw_param[0],'/');
		if (get_config('UrlReWrite')) {
			//开启urlrewrite
			$param		= explode('/', $first_param);
		} else {
			//未开启urlrewrite
			if(empty($first_param)){
				$param[0]='';
				$param[1]='';
				$c = get_config('ControllerTrigger');
				$m = get_config('FunctionTrigger');
				foreach($_GET as $key=>$value){
					if($key == $c){
						$param[0] = $value;
					}elseif($key == $m){
						$param[1] = $value;
					}else{
						$param[] = $key;
						$param[] = $value;
					}
					
				}
			}else{
				$param = explode('/', $first_param);
			}
		}
		
		$this->controller = get_config('default_controller');//设置默认控制器
		$this->action = 'index';//设置默认执行Action
		if(!empty($param[0])){
				//确定Action
				(!empty($param[0]) && in_array(strtolower($param[0]), $this->blockAction)) && show_error($param[0].'是关键字，不能作为类名!');
				(!empty($param[1]) && in_array(strtolower($param[1]), $this->blockActionDo)) && show_error($param[1].'是关键字，不能作为函数名!');
				if (!empty($param[0]) && strtolower($param[0]) != 'index.php') {
					$this->controller = $param[0];
				}
				//确定ActionDo
				if(!empty($param[1])){
					$this->action = $param[1];
				}

				//替换到最后的.html
				get_config('UrlHtml') && array_splice($param, -1, 1, str_ireplace('.html', '', end($param)));
				//重组参数
				$count = count($param);
				for ($i=2;$i<$count;$i++) {
					$this->param[$param[$i]] = @$param[++$i];
				}
		}
	}

	/**
	 * 根据解析的URL获取Controller文件
	 *
	 * @return void
	 * @access private
	 *
	 */
	private function getControllerFile(){
		$this->controllerFile = BASE_CONTROLLER . strtolower($this->controller) . '.php';
		if(!is_file($this->controllerFile)) {
			show_error("错误的请求，找不到Controller文件($this->controllerFile)");
		} else {
			include_once($this->controllerFile);
		}
	}

	/**
	 * 根据Controller文件名获取Controller类名并且执行
	 *
	 * @return void
	 * @access private
	 *
	 */
	private function getControllerClass(){
		$this->controllerClass = $this->controller . 'Controller';
		if(!class_exists($this->controllerClass)) {
			show_error("错误的请求，找不到Controller类($this->controllerClass)");
		} else {
			$methods = get_class_methods($this->controllerClass);
			if(in_array($this->action, $methods, true)){
				new HttpRequest($this->param);
			}else{
				show_error("错误的请求，在Controller类($this->controllerClass)中没有找到Method(".$this->action.")");
			}
		}
	}
}